# from ultralytics import YOLO
#
# yolo = YOLO('./yolov8l.pt',task='detect')
#
# # result = yolo(source="./2.mp4",save=True,conf=0.8,show=True)
# result = yolo(source="./4.mp4",save=True,conf=0.7,show=True)
# result1 = result[0]
# print(result1)



from ultralytics import YOLO

# if __name__ == '__main__':
#     # 加载一个模型
#     # model = YOLO('yolov8n.pt')  # 从YAML建立一个新模型
#     # # 训练模型
#     # results = model.train(
#     #     data="./Myvoc.yaml",
#     #     device='0',
#     #     epochs=5,
#     #     batch=4,
#     #     verbose=False,
#     #     imgsz=640)
#
#     model = YOLO('yolov8l.pt')
#
#     model.train(data="./Myvoc.yaml")
#


# import matplotlib.pyplot as plt
#
# # plt.show(result[0].plot())

from ultralytics import YOLO
from PLCconnection import PLCconnect
# yolo = YOLO('./yolov8n.pt',task='detect')
yolo = YOLO('./yolov8l.pt',task='detect')
results = yolo(source="./4.mp4",save=True,conf=0.7,show=True,stream=True)
# result1 = results[0]
# print(result1)
for r in results:
        boxes = r.boxes  # Boxes object for bbox outputs
        masks = r.masks  # Masks object for segment masks outputs
        probs = r.probs  # Class probabilities for classification outputs
        print(boxes)
        # PLCconnect()
        print("----------------------------------------------------------------")
# from ultralytics import YOLO
# from ultralytics.solutions import speed_estimation
# import cv2
# model = YOLO("yolov8l.pt")
# names = model.model.names
#
# cap = cv2.VideoCapture("./2.mp4")
# assert cap.isOpened(), "Error reading video file"
# w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
#
# # Video writer
# video_writer = cv2.VideoWriter("speed_estimation.avi",
#                                cv2.VideoWriter_fourcc(*'mp4v'),
#                                fps,
#                                (w, h))
#
# # line_pts = [(0, 360), (1280, 360)]
# # line_pts = [(360, 0), (360, 1280)]
#
# # Init speed-estimation obj
# speed_obj = speed_estimation.SpeedEstimator()
#
# speed_obj.set_args(reg_pts=line_pts,
#                    names=names,
#                    view_img=True)
#
# while cap.isOpened():
#
#     success, im0 = cap.read()
#     if not success:
#         print("Video frame is empty or video processing has been successfully completed.")
#         break
#
#     tracks = model.track(im0, persist=True, show=False)
#
#     im0 = speed_obj.estimate_speed(im0, tracks)
#     # print(im0)
#     video_writer.write(im0)
#
# cap.release()
# video_writer.release()
# cv2.destroyAllWindows()